package com.ibiz.znjd.plugin;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.ibiz.znjd.MainActivity;
import com.ibiz.znjd.MainApplication;
import com.ibiz.znjd.baidu.location.GeoFenceConfig;
import com.ibiz.znjd.baidu.location.GeoFenceService;
import com.ibiz.znjd.znjd.RecordPlanUtil;
import com.ibiz.znjd.znjd.WaringUtil;

@NativePlugin()
public class CommonPlugin  extends Plugin {
    /**
     * 应用对象
     */
    private MainApplication app;

    @Override
    public void load(){
        // 初始化app
        app= ((MainApplication)(((MainActivity)getContext()).getApplication()));
    }

    /**
     * 设置全局环境变量
     * @param call
     */
    @PluginMethod()
    public void setEnvironment(PluginCall call) {
//        String baseUrl = "http://101.132.236.47:58950"; // 开发环境
        String baseUrl = "http://36.139.5.1:8084";  // 生产环境
        app.envMap.put("locusUrl",baseUrl+call.getString("locusUrl"));
        app.envMap.put("warningUrl",baseUrl+call.getString("warningUrl"));
        app.envMap.put("personUrl",baseUrl+call.getString("personUrl"));
        app.envMap.put("recordUrl",baseUrl+call.getString("recordUrl"));
        app.envMap.put("token",call.getString("token"));
        app.envMap.put("personid",call.getString("personid"));
        app.envMap.put("photo",call.getString("photo"));
        // 监管报道提前量
        if(null != (call.getInt("bdsjBefore")) ){
            app.envMap.put("bdsjBefore",Integer.toString((call.getInt("bdsjBefore"))));
        }
        // 监管方法，10 手环，20 手机， 30 手环+手机
        app.envMap.put("jgfs",call.getString("jgfs"));

        // 开启定时 TODO
//        ((MainApplication) app).foregroundService.startInternal();
        ((MainApplication) app).lastRecordTime = 0;

        // 设置监管开始结束时间
        WaringUtil.setStartAndEnd(call.getString("jgkssj"),call.getString("jgjssj"));
        // 初始化地理围栏
        GeoFenceConfig fenceConfig = new GeoFenceConfig();
        String fenceType = call.getString("fenceType");
        if("district".equals(fenceType)){
            fenceConfig.setParam("fenceType", GeoFenceConfig.FenceType.DISTRICT);
            fenceConfig.setParam("areaName", call.getString("areaName") );
            System.out.println("围栏信息：areaName:"+call.getString("areaName"));
        }else if("circle".equals(fenceType)){
            fenceConfig.setParam("fenceType", GeoFenceConfig.FenceType.CIRCLE);
            fenceConfig.setDPoint(call.getDouble("lat"), call.getDouble("lng"));
            fenceConfig.setParam("aroundRadius", call.getFloat("aroundRadius"));
            System.out.println("围栏信息：lat:"+call.getDouble("lat")+",lng:"+call.getDouble("lng")+",aroundRadius:"+call.getFloat("aroundRadius"));
        }
        if(fenceConfig.isValid()){
            WaringUtil.setGeoFenceConfig(fenceConfig, app);
        }

        // 初始化报到数据
        RecordPlanUtil.init(((MainActivity)getContext()));

        call.resolve();
    }

    /**
     * 获取应用程序的版本信息
     * @param call
     */
    @PluginMethod()
    public void getAppVersion(PluginCall call) {
        try {
            PackageManager pm = getContext().getPackageManager();
            PackageInfo pi = pm.getPackageInfo(getContext().getPackageName(), 0);
            String versionName = pi.versionName;
            int versionCode = pi.versionCode;
            JSObject ret = new JSObject();
            ret.put("versionCode",versionCode);
            ret.put("versionName",versionName);
            call.resolve(ret);
        } catch (Exception e) {
            System.out.println("VersionInfo Exception");
        }
    }
}
